package com.zhangyu.shadow.repair.delegate.luncher;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.joanzapata.iconify.widget.IconTextView;
import com.zhangyu.shadow.repair.R;
import com.zhangyu.shadow.repair.R2;
import com.zhangyu.shadow.repair.delegate.bean.DeptBean;
import com.zhangyu.shadow.repair.delegate.bean.SetInfoBean;
import com.zhangyu.shadow.repair.delegate.dept.DeptDelegate;
import com.zhangyu.shadow.repair.delegate.irregular.IrregularBusDelegate;
import com.zhangyu.shadow.repair.delegate.maintain.MaintainDelegate;
import com.zhangyu.shadow.repair.delegate.setting.SettingDelegate;
import com.zhangyu.shadow.repair.event.ChooseSearchBusEvent;
import com.zhangyu.shadow.repair.event.LogoutEvent;
import com.zhangyu.shadow.repair.event.OrgResetEvent;
import com.zhangyu.shadow.repair.manager.AccountManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.OnClick;
import st.lib.base.delegate.mvp.MvpDelegate;
import st.lib.base.ui.progress.sweetalert.DialogLoader;
import st.lib.base.ui.progress.sweetalert.IDialogClickListener;
import st.lib.base.util.utilcode.util.ScreenUtils;

/**
 * @author zhangyu
 * @date 2018/2/7
 */

public class LauncherDelegate extends MvpDelegate<LauncherPresenter> implements LauncherContract.View {

    @BindView(R2.id.icon_repair)
    IconTextView mIconRepair;
    @BindView(R2.id.tv_repair)
    TextView mTvRepair;
    @BindView(R2.id.card_repair)
    CardView mCardRepair;
    @BindView(R2.id.icon_ve)
    IconTextView mIconVe;
    @BindView(R2.id.tv_ve)
    TextView mTvVe;
    @BindView(R2.id.card_fix)
    CardView mCardFix;
    @BindView(R2.id.icon_history)
    IconTextView mIconHistory;
    @BindView(R2.id.tv_history)
    TextView mTvHistory;
    @BindView(R2.id.card_complete)
    CardView mCardComplete;
    @BindView(R2.id.icon_setting)
    IconTextView mIconSetting;
    @BindView(R2.id.tv_setting)
    TextView mTvSetting;
    @BindView(R2.id.card_setting)
    CardView mCardSetting;

    public static LauncherDelegate newInstance() {
        return new LauncherDelegate();
    }

    private DialogLoader mDialogLoader;


    @Override
    public Object setLayout() {
        return R.layout.delegate_launcher;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        if (!ScreenUtils.isPad(_mActivity)) {
            resetSize(mCardRepair, mIconRepair, mTvRepair);
            resetSize(mCardFix, mIconVe, mTvVe);
            resetSize(mCardComplete, mIconHistory, mTvHistory);
            resetSize(mCardSetting, mIconSetting, mTvSetting);
        }

        mDialogLoader = new DialogLoader(getContext());
        EventBus.getDefault().register(this);
        verificationDeptOrg();
    }

    private void resetSize(CardView cardView, IconTextView icon, TextView textView) {
        ViewGroup.LayoutParams layoutParamsRepair = cardView.getLayoutParams();
        layoutParamsRepair.width = ScreenUtils.getScreenWidth() / 3;
        icon.setTextSize(TypedValue.COMPLEX_UNIT_SP, 50);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    }

    @OnClick(R2.id.card_repair)
    public void onClickRepair(View view) {
        RepairDialog.newInstance(0).show(
                getChildFragmentManager(), "RepairDialog"
        );
    }

    @OnClick(R2.id.card_fix)
    public void onClickFix(View view) {
        RepairDialog.newInstance(1).show(
                getChildFragmentManager(), "RepairDialog"
        );
    }

    private void verificationDeptOrg() {
        mPresenter.getPersonInfo();
    }

    @OnClick(R2.id.card_setting)
    public void onSettingClick() {
        start(SettingDelegate.newInstance());
    }

    @OnClick(R2.id.card_complete)
    void onComplete() {
        start(IrregularBusDelegate.create());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void showLoading(String str) {
        mDialogLoader.showProgressDialog(true,
                getString(R.string.loading_get_dept_org),
                getString(R.string.loading_str_waiting));
    }

    @Override
    public void hideLoading() {
        mDialogLoader.clearDialog();
    }

    @Override
    public void getPersonInfoSucceed(SetInfoBean infoBean) {
        try {
            if (infoBean.getGroup() == null || Integer.parseInt(infoBean.getGroup().getId()) <= 0) {
                //没有设置组织关系，就跳转到页面去设置
                start(DeptDelegate.newInstance(false));
            } else {
                //缓存起来
                DeptBean deptBean = infoBean.getDept();
                DeptBean org = infoBean.getOrg();
                AccountManager.getInstance().setDeptName(deptBean == null ? "" : deptBean.getName());
                AccountManager.getInstance().setDepId(deptBean == null ? "" : deptBean.getId());
                AccountManager.getInstance().setGroupName(infoBean.getGroup().getName());
                AccountManager.getInstance().setGroupId(infoBean.getGroup().getId());
                AccountManager.getInstance().setOrgId(org == null ? "" : org.getId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void getPersonInfoFailed(int code, String msg) {
        mDialogLoader.stopProgressWithFailed(msg, null, new IDialogClickListener() {
            @Override
            public void onClick() {
                EventBus.getDefault().post(new LogoutEvent());
            }
        });
    }

    @Override
    public LauncherPresenter getPresenter() {
        return new LauncherPresenter(this);
    }

    @Subscribe
    public void onBusChooseEvent(ChooseSearchBusEvent event) {
        start(MaintainDelegate.create(event.mMultipleItemEntity));
    }

    @Subscribe
    public void onOrgReset(OrgResetEvent event) {
        mPresenter.getPersonInfo();
        mPresenter.getCanEditTime();
    }
}
